#include <QtGui>
#include <QtNetwork>
#include "ftpwindow.h"
#include "about.h"

/* Layout */

FtpWindow::FtpWindow(QWidget *parent)
    : QDialog(parent), ftp(0), networkSession(0)

{
    createMenu();

    /* Właściwości poszczególnych obiektów  */

    ftpServerLabel = new QLabel(tr(" Nazwa serwera FTP :"));
    ftpServerLabel->setFixedWidth(120);

    ftpServerLineEdit = new QLineEdit("ftp.politechnika.lublin.pl");
    ftpServerLineEdit->setFixedWidth(200);
    ftpServerLabel->setBuddy(ftpServerLineEdit);

    ftpServerLabelUsername = new QLabel(QString :: fromUtf8(" Nazwa użytkownika :"));
    ftpServerLineEditUsername = new QLineEdit(tr("id2sieci"));

    ftpServerLabelPassword = new QLabel(QString :: fromUtf8(" Hasło:"));
    ftpServerLineEditPassword= new QLineEdit("id2sieci06");
    ftpServerLineEditPassword->setEchoMode(QLineEdit :: Password);

    ftpServerLabelRename = new QLabel(QString :: fromUtf8("Podaj nową nazwę : "));
    ftpServerLineEditRename = new QLineEdit(QString :: fromUtf8("Nowa_nazwa"));

    ftpServerLabelMakeDir = new QLabel(QString :: fromUtf8("Podaj nazwę katalogu : "));
    ftpServerLineEditMakeDir = new QLineEdit(QString :: fromUtf8("Nazwa_katalogu"));

    ftpServerLabelPort = new QLabel(tr(" Numer portu :"));
    ftpServerLineEditPort = new QLineEdit("21");

    imageLabel = new QLabel(tr("<img src=:/images/logok.png >"));
    imageLabel2 = new QLabel(tr("<img src=:/images/client.png >"));
    imageLabel2 -> setAlignment(Qt::AlignRight);

    statusLabel = new QLabel(QString :: fromUtf8("Proszę podać nazwę serwera FTP , login oraz hasło do twojego konta FTP"));

#ifdef Q_OS_SYMBIAN

    /* Włączanie word wrapping [ aby zmieścić text na ekranie smartphona ] */

    statusLabel->setWordWrap( true );

#endif

    fileList = new QTreeWidget;

    fileList->setEnabled(false);
    fileList->setRootIsDecorated(false);
    fileList->setHeaderLabels(QStringList() << tr("Nazwa") << tr("Rozmiar") << QString :: fromUtf8("Właściciel") << tr("Grupa") << tr("Czas Edycji"));
    fileList->header()->setStretchLastSection(false);


    connectButton = new QPushButton(QIcon(":/images/connect.png"),QString :: fromUtf8("Połącz"));
    connectButton->setDefault(true);

    cdToParentButton = new QPushButton;
    cdToParentButton->setIcon(QPixmap(":/images/cdtoparent.png"));
    cdToParentButton->setEnabled(false);


    downloadButton = new QPushButton(tr("Pobierz plik"));
    downloadButton->setIcon(QPixmap(":/images/download.png"));
    downloadButton->setEnabled(false);

    quitButton = new QPushButton(QIcon(":/images/exit.png"),QString :: fromUtf8("Wyjdź"));

    makeDirButton = new QPushButton(QString :: fromUtf8("Utwórz katalog"));
    makeDirButton->setIcon(QPixmap(":/images/add.png"));
    makeDirButton->setEnabled(false);

    deleteButton = new QPushButton(QString :: fromUtf8("Usuń Plik / Katalog"));
    deleteButton->setIcon(QPixmap(":/images/delete2.png"));
    deleteButton->setEnabled(false);

    uploadButton= new QPushButton(QString :: fromUtf8("Upload Pliku"));
    uploadButton->setIcon(QPixmap(":/images/upload.png"));
    uploadButton->setEnabled(false);

    renameButton = new QPushButton(QString :: fromUtf8("Zmień nazwę"));
    renameButton->setIcon(QPixmap(":/images/rename.png"));
    renameButton->setEnabled(false);

    buttonBox = new QDialogButtonBox;
    buttonBox->addButton(downloadButton, QDialogButtonBox::ActionRole);
    buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);

    buttonDefaultPort = new QPushButton(QString :: fromUtf8("Przywróć domyslny port"));
    buttonDefaultPort->setIcon(QPixmap(":/images/port.png"));
    buttonDefaultPort->setEnabled(true);

    progressDialog = new QProgressDialog(this);

    /*Połącznie obiektów + akcje (funkcje) */

    connect(fileList, SIGNAL(itemActivated(QTreeWidgetItem*,int)),
            this, SLOT(processItem(QTreeWidgetItem*,int)));
    connect(fileList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)),
            this, SLOT(enableDownloadButton()));
    connect(progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
    connect(connectButton, SIGNAL(clicked()), this, SLOT(connectOrDisconnect()));
    connect(cdToParentButton, SIGNAL(clicked()), this, SLOT(cdToParent()));
    connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadFile()));
    connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(renameButton, SIGNAL(clicked()), this, SLOT(changename()));
    connect(makeDirButton, SIGNAL(clicked()), this, SLOT(makedir()));
    connect(deleteButton, SIGNAL(clicked()), this, SLOT(deletefiles()));
    connect(uploadButton, SIGNAL(clicked()), this, SLOT(uploadFile()));
    connect(buttonDefaultPort, SIGNAL(clicked()), this, SLOT(defaultPort()));

    /* Akcje menu */

    connect(aboutAct, SIGNAL(triggered()),this, SLOT(about()));
    connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
    connect(renameAct, SIGNAL(triggered()), this, SLOT(changename()));
    connect(deleteAct, SIGNAL(triggered()),this, SLOT(deletefiles()));
    connect(makeDirAct, SIGNAL(triggered()), this, SLOT(makedir()));
    connect(downloadAct, SIGNAL(triggered()), this, SLOT(downloadFile()));
    connect(connectAct, SIGNAL(triggered()), this, SLOT(connectOrDisconnect()));
    connect(uploadAct, SIGNAL(triggered()), this, SLOT(uploadFile()));
    connect(exitAction, SIGNAL(triggered()), this, SLOT(accept()));

    /* Właściwości akcje menu  */

    deleteAct->setVisible(false);
    renameAct->setVisible(false);
    makeDirAct->setVisible(false);
    downloadAct->setVisible(false);
    uploadAct->setVisible(false);
    connectAct->setEnabled(true);

    /* Tworzenie Layoutu  [ Układ ]*/

    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->setMenuBar(menuBar);
    mainLayout->addWidget(imageLabel,0,0,1,2);
    mainLayout->addWidget(imageLabel2,0,3);
    mainLayout->addWidget(ftpServerLabel,1,0);
    mainLayout->addWidget(ftpServerLineEdit,1,1);
    mainLayout->addWidget(ftpServerLabelUsername, 1,2);
    mainLayout->addWidget(ftpServerLineEditUsername,1,3);
    mainLayout->addWidget(ftpServerLabelPassword,2,0);
    mainLayout->addWidget(ftpServerLineEditPassword,2,1);
    mainLayout->addWidget(cdToParentButton,2,2);
    mainLayout->addWidget(connectButton,2,3);
    mainLayout->addWidget(fileList,4,0,1,4);
    mainLayout->addWidget(statusLabel,5,0,1,4);
    mainLayout->addWidget(ftpServerLabelRename,6,0);
    mainLayout->addWidget(ftpServerLineEditRename,6,1);
    mainLayout->addWidget(renameButton,6,2);
    mainLayout->addWidget(deleteButton,6,3);
    mainLayout->addWidget(ftpServerLabelMakeDir,8,0);
    mainLayout->addWidget(ftpServerLineEditMakeDir,8,1);
    mainLayout->addWidget(makeDirButton,8,2);
    mainLayout->addWidget(uploadButton,8,3);
    mainLayout->addWidget(ftpServerLabelPort,9,0);
    mainLayout->addWidget(ftpServerLineEditPort,9,1);
    mainLayout->addWidget(buttonDefaultPort,9,2);
    mainLayout->addWidget(buttonBox,9,3);
    setLayout(mainLayout);



    /* Konfiguracja sieciowa QNetwork*/


    QNetworkConfigurationManager manager;
    if (manager.capabilities() & QNetworkConfigurationManager::NetworkSessionRequired) {

        // Pobierz zapisaną konfigurację sieciową

        QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
        settings.beginGroup(QLatin1String("QtNetwork"));
        const QString id = settings.value(QLatin1String("DefaultNetworkConfiguration")).toString();
        settings.endGroup();

        // Jeżeli zapisana konfiguracja nie jest dostępna , uzywamy konfiguracji systemowej

        QNetworkConfiguration config = manager.configurationFromIdentifier(id);
        if ((config.state() & QNetworkConfiguration::Discovered) !=
            QNetworkConfiguration::Discovered) {
            config = manager.defaultConfiguration();
        }

        networkSession = new QNetworkSession(config, this);
        connect(networkSession, SIGNAL(opened()), this, SLOT(enableConnectButton()));

        connectButton->setEnabled(false);
        connectAct->setEnabled(false);
        statusLabel->setText(tr("Otwieranie nowej sesji sieciowej."));
        networkSession->open();
    }


    /* Właściwości Okno główne aplikacji */

    setWindowTitle(tr("Klient FTP"));
    setWindowIcon(QIcon(":/images/logo.png"));
}

/* Ustawiamy rozmiar okna podczas uruchomienia */

QSize FtpWindow::sizeHint() const
{
    return QSize(0,600);
}

/* Definicja akcji menu [O Aplikacji] */

void FtpWindow::about()
    {

        About().exec();
    }

/* Domyslny port */

void FtpWindow::defaultPort()
    {
ftpServerLineEditPort->setText("21");
    }

/* Definicja Akcji [O Qt 4.7 ]*/


void FtpWindow::aboutQt()
    {
        infoLabel->setText(tr("Invoked <b> Help|About Qt </b>"));
    }

/* Tworzymy Menu :)  */

void FtpWindow::createMenu()
{
    menuBar = new QMenuBar;
    fileMenu = new QMenu(tr("&Plik"),this);
    deleteAct = fileMenu->addAction(QIcon(":/images/delete2.png"),QString :: fromUtf8("Usuń Plik / Katalog"));
    makeDirAct= fileMenu->addAction(QIcon(":/images/add.png"),tr("Dodaj katalog"));
    renameAct= fileMenu->addAction(QIcon(":/images/rename.png"),QString :: fromUtf8("Zmień nazwę"));
    downloadAct= fileMenu->addAction(QIcon(":/images/download.png"),QString :: fromUtf8("Pobierz Plik"));
    uploadAct= fileMenu->addAction(QIcon(":/images/upload.png"),QString :: fromUtf8("Wyślij Plik"));
    connectAct= fileMenu->addAction(QIcon(":/images/connect.png"),QString :: fromUtf8("Połącz / Rozłącz"));
    fileMenu->addSeparator();
    exitAction = fileMenu->addAction(QIcon(":/images/exit.png"),tr("Zamknij Program"));
    menuBar->addMenu(fileMenu);

    helpMenu = new QMenu(tr("&Pomoc"),this);
    aboutAct = helpMenu->addAction(QPixmap(":/images/info.png"),tr("O Aplikacji"));
    aboutQtAct = helpMenu->addAction(QIcon(":/images/info.png"),tr("Informacje o Qt"));
    helpMenu->addSeparator();
    helpAct = helpMenu->addAction(QIcon(":/images/help.png"),tr("Pomoc"));
    menuBar->addMenu(helpMenu);

}

/*   Połączenie FTP   */

void FtpWindow::connectOrDisconnect()
{
    if (ftp) {
        ftp->abort();
        ftp->deleteLater();
        ftp = 0;

        fileList->setEnabled(false);
        cdToParentButton->setEnabled(false);
        downloadButton->setEnabled(false);
        downloadAct->setVisible(false);
        uploadAct->setVisible(false);
        makeDirButton->setEnabled(false);
        renameButton->setEnabled(false);
        uploadButton->setEnabled(false);
        deleteButton->setEnabled(false);
        deleteAct->setVisible(false);
        renameAct->setVisible(false);
        makeDirAct->setVisible(false);
        connectButton->setEnabled(true);
        connectAct->setEnabled(true);
        connectButton->setText(QString :: fromUtf8("Połącz"));
#ifndef QT_NO_CURSOR
        setCursor(Qt::ArrowCursor);
#endif
        statusLabel->setText(QString :: fromUtf8( "Proszę podać nazwę serwera FTP , nazwę uzytkownika , oraz hasło ... "));
        return;
    }

#ifndef QT_NO_CURSOR
    setCursor(Qt::WaitCursor);
#endif


    ftp = new QFtp(this);
    connect(ftp, SIGNAL(commandFinished(int,bool)),
            this, SLOT(ftpCommandFinished(int,bool)));
    connect(ftp, SIGNAL(listInfo(QUrlInfo)),
            this, SLOT(addToList(QUrlInfo)));
    connect(ftp, SIGNAL(dataTransferProgress(qint64,qint64)),
            this, SLOT(updateDataTransferProgress(qint64,qint64)));
    fileList->clear();
    currentPath.clear();
    isDirectory.clear();

    QString mystring = ftpServerLineEditPort->text();
    int portNumber = mystring.toInt();


    QUrl url(ftpServerLineEdit->text());
    if (!url.isValid() || url.scheme().toLower() != QLatin1String("ftp")) {
        ftp->connectToHost(ftpServerLineEdit->text(),portNumber);
       // ftp->login();
        ftp->login(ftpServerLineEditUsername->text(),ftpServerLineEditPassword->text());
    } else {
        ftp->connectToHost(url.host(), url.port(portNumber));

        if (!url.userName().isEmpty())
        // ftp->login(QUrl::fromPercentEncoding(url.userName().toLatin1()), url.password());
         ftp->login(ftpServerLineEditUsername->text(),ftpServerLineEditPassword->text());
        else    ftp->login();
        if (!url.path().isEmpty())
            ftp->cd(url.path());
    }

    fileList->setEnabled(true);
    connectButton->setEnabled(false);
    connectAct->setEnabled(false);
    connectButton->setText(QString :: fromUtf8("Rozłącz"));
    statusLabel->setText(QString :: fromUtf8("Trwa łaczenie z serwerem FTP %1...")
                         .arg(ftpServerLineEdit->text()));
}

/* Zmiana nazwy */

void FtpWindow::changename()
{
     QString fileNameCurrent = fileList->currentItem()->text(0);
     QString fileNameNew = ftpServerLineEditRename->text();
     ftp->rename(fileNameCurrent ,fileNameNew);
     statusLabel->setText(QString :: fromUtf8( "Zmieniono nazwę pliku ... "));
     QMessageBox::information(this, tr("Powiadomienie :"),
     QString :: fromUtf8("Zmieniono nazwę pliku z %1 na %2 .")
                              .arg(fileNameCurrent)
                              .arg(fileNameNew));
     fileList->clear();
     ftp->list();

}

/* Usuwanie plików katalogów */

void FtpWindow::deletefiles()
{
    QTreeWidgetItem *current = fileList->currentItem();
    if (current)
        {
              QString dirNameToDel = fileList->currentItem()->text(0);
              ftp->rmdir(dirNameToDel);
              statusLabel->setText(QString :: fromUtf8( "Usunięto plik ... "));
              QMessageBox::information(this, tr("Powiadomienie :"),
              QString :: fromUtf8("Katalog o nazwie %1 został usuniety .")
                                 .arg(dirNameToDel));

         } /*Jeśli plik ( nie katalog) */
            else
                    {
                         QString fileNameDel = fileList->currentItem()->text(0);
                            ftp->remove(fileNameDel);
                            statusLabel->setText(QString :: fromUtf8( "Usunięto plik ... "));
                            QMessageBox::information(this, tr("Powiadomienie :"),
                            QString :: fromUtf8("Pilk o nazwie %1 został usuniety .")
                                                                                       .arg(fileNameDel));
         }

    fileList->clear();
    ftp->list();
}

/* Usuwanie rekurencyjne katalogu u jego zawartości */

void FtpWindow::removeDirF()
{
    QString dirName= fileList->currentItem()->text(0);
    bool t=false;
    for(int i=1;t;i++)
    {
    t = removeDir(dirName);
    }
}

bool FtpWindow::removeDir(const QString &dirName)
{
    bool result = true;
    QDir dir(dirName);

    if (dir.exists(dirName)) {
        Q_FOREACH(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden  | QDir::AllDirs | QDir::Files, QDir::DirsFirst)) {
            if (info.isDir()) {
                result = removeDir(info.absoluteFilePath());
            }
            else {
                result = QFile::remove(info.absoluteFilePath());
            }

            if (!result) {
                return result;
            }
        }
        result = dir.rmdir(dirName);
    }

    return result;
}

/*  Nowy Katalog */

void FtpWindow::makedir()
{
     QString CurrentDirName = ftpServerLineEditMakeDir->text();
     // currentPath += '/';
     ftp->mkdir(CurrentDirName);

     statusLabel->setText(QString :: fromUtf8( "Utworzono katalog ... "));
     QMessageBox::information(this, tr("Powiadomienie :"),
     QString :: fromUtf8("Utworzono nowy katalog o nazwie %1 . %2")
                              .arg(CurrentDirName).arg(currentPath));
     fileList->clear();
     ftp->list();

}


/* Pobieranie pliku */


void FtpWindow::downloadFile()
{


    QString fileDirectory = QFileDialog::getExistingDirectory(
            this,
                QString :: fromUtf8("Okno zapis plików"),
                QString :: fromUtf8("Wybierz katalog do zapisu plików"),QFileDialog::ShowDirsOnly);

    QString fileName = fileDirectory;
    fileName +="/";
    fileName += fileList->currentItem()->text(0);
    QString fileNames = fileList->currentItem()->text(0);
    if (QFile::exists(fileName)) {
        QMessageBox::warning(this, tr("FTP"),
                                 QString :: fromUtf8("<b> Nie można skopiować pliku !</b> "
                                                     "<br> W katalogu %2 istnieje plik o nazwie %1  ")
                                 .arg(fileNames).arg(fileDirectory));
        return;
    }


    file = new QFile(fileName);

    if (!file->open(QIODevice::WriteOnly)) {
        QMessageBox::warning(this, tr("Uwaga !"),
                                 QString :: fromUtf8("Nie można zapisac pliku %1: %2.")
                                 .arg(fileNames).arg(file->errorString()));
        delete file;
        return;
    }

    ftp->get(fileList->currentItem()->text(0), file);
    progressDialog->setLabelText(tr("Trwa pobieranie  %1...").arg(fileNames));
    downloadButton->setEnabled(false);
    downloadAct->setVisible(false);
    uploadAct->setVisible(false);
    progressDialog->exec();
}


/* Upload plików */


void FtpWindow::uploadFile()
{   QString fileName ="";
    fileName = QFileDialog::getOpenFileName(
            this,
                QString :: fromUtf8("Wybierz plik do wysłania"),
                QString :: fromUtf8("Pliki o takiej samej nazwie zostaną nadpisane"));
   if (fileName=="")
   {
       statusLabel->setText(QString :: fromUtf8( " Nie wybrano pliku "));
   } else{
   QFile *file = new QFile( fileName );
   QFileInfo fi( fileName );
   ftp->put(file, fi.fileName());
   statusLabel->setText(QString :: fromUtf8( "Wysłano Plik %1 ")
                        .arg(fileName));
   }
  fileList->clear();
  ftp->list();
}


/*  Anulowanie pobierania plików  */


void FtpWindow::cancelDownload()
{
    ftp->abort();

    if (file->exists()) {
        file->close();
        file->remove();
    }
    delete file;
}


/* Sprawdzamy czy połączenie jest ok */


void FtpWindow::ftpCommandFinished(int, bool error)
{
#ifndef QT_NO_CURSOR
    setCursor(Qt::ArrowCursor);
#endif

    if (ftp->currentCommand() == QFtp::ConnectToHost) {
        if (error) {
            QMessageBox::critical(this, QString :: fromUtf8(" Błąd podczas próby połączenia !"),
                                     QString :: fromUtf8("Nie można połączyć z serwerem FTP "
                                        " %1. Proszę sprawdzić czy "
                                        "nazwa serwera jest poprawna?")
                                     .arg(ftpServerLineEdit->text()));
            connectOrDisconnect();
            return;
        }
        statusLabel->setText(QString :: fromUtf8("Połączono z %1.")
                             .arg(ftpServerLineEdit->text()));
        fileList->setFocus();
        downloadButton->setDefault(true);
        downloadAct->setVisible(true);
        uploadAct->setVisible(true);
        makeDirButton->setEnabled(true);
        renameButton->setEnabled(true);
        uploadButton->setEnabled(true);
        deleteButton->setEnabled(true);
        connectButton->setEnabled(true);
        connectAct->setEnabled(true);
        deleteAct->setVisible(true);
        renameAct->setVisible(true);
        makeDirAct->setVisible(true);
        return;
    }

    if (ftp->currentCommand() == QFtp::Login)
        ftp->list();


    if (ftp->currentCommand() == QFtp::Get) {
        if (error) {
            statusLabel->setText(QString :: fromUtf8("Anulowano pobieranie pliku %1.")
                                 .arg(file->fileName()));
            file->close();
            file->remove();
        } else {
            statusLabel->setText(QString :: fromUtf8("Pobrano %1 do katalogu.")
                                 .arg(file->fileName()));
            file->close();
        }
        delete file;
        enableDownloadButton();
        progressDialog->hide();


 /* Komunikat błędne dane [login / hasło ] */


    } else if (ftp->currentCommand() == QFtp::List) {
        if (isDirectory.isEmpty()) {
            fileList->addTopLevelItem(new QTreeWidgetItem(QStringList() << QString :: fromUtf8("Błąd")));
            statusLabel->setText(QString :: fromUtf8("Sprawdź login i hasło"));
            QMessageBox::critical(this,QString :: fromUtf8("Błąd podczas logowania ! "),
                                     QString :: fromUtf8(" <center>   Nie można połączyć z serwerem FTP :    "
                                        " <b> %1 </b> <br> Proszę sprawdzić "
                                        "hasło i nazwę użytkownika ! ")
                                     .arg(ftpServerLineEdit->text()));
            connectOrDisconnect();
            fileList->setEnabled(false);
        }
    }

}


/*Wyświetlanie plików i katalogów w tabelce FTP*/


void FtpWindow::addToList(const QUrlInfo &urlInfo)
{
    QTreeWidgetItem *item = new QTreeWidgetItem;
    item->setText(0, urlInfo.name());
    item->setText(1, QString::number(urlInfo.size()));
    item->setText(2, urlInfo.owner());
    item->setText(3, urlInfo.group());
    item->setText(4, urlInfo.lastModified().toString("MMM dd yyyy"));

    QPixmap pixmap(urlInfo.isDir() ? ":/images/dir.png" : urlInfo.isExecutable() ?  ":/images/executable.png" :  ":/images/file.png" );
    item->setIcon(0, pixmap);

    isDirectory[urlInfo.name()] = urlInfo.isDir();
    fileList->addTopLevelItem(item);
    if (!fileList->currentItem()) {
        fileList->setCurrentItem(fileList->topLevelItem(0));
        fileList->setEnabled(true);
    }
}

/* funkcja wchodzenie w katalogach katalog głębiej */

void FtpWindow::processItem(QTreeWidgetItem *item, int /*column*/)
{
    QString name = item->text(0);
    if (isDirectory.value(name)) {
        fileList->clear();
        isDirectory.clear();
        currentPath += '/';
        currentPath += name;
        ftp->cd(name);
        ftp->list();
        cdToParentButton->setEnabled(true);
#ifndef QT_NO_CURSOR
        setCursor(Qt::WaitCursor);
#endif
        return;
    }
}


/* Cofanie sie w katalogach o jeden wyżej*/


void FtpWindow::cdToParent()
{
#ifndef QT_NO_CURSOR
    setCursor(Qt::WaitCursor);
#endif
    fileList->clear();
    isDirectory.clear();
    currentPath = currentPath.left(currentPath.lastIndexOf('/'));
    if (currentPath.isEmpty()) {
        cdToParentButton->setEnabled(false);
        ftp->cd("/");
    } else {
        ftp->cd(currentPath);
    }
    ftp->list();
}


/* Odświerzanie postepu pobieranie pliku */


void FtpWindow::updateDataTransferProgress(qint64 readBytes,
                                           qint64 totalBytes)
{
    progressDialog->setMaximum(totalBytes);
    progressDialog->setValue(readBytes);
}



/* Funkcja kontrolująca (blokująca ) przycisk "Pobierz" */


void FtpWindow::enableDownloadButton()
{
    QTreeWidgetItem *current = fileList->currentItem();
    if (current) {
        QString currentFile = current->text(0);
        downloadButton->setEnabled(!isDirectory.value(currentFile));
        downloadAct->setVisible(!isDirectory.value(currentFile));
    } else {
        downloadButton->setEnabled(false);
            downloadAct->setVisible(false);
    }
}


/* Włączanie przycisku Połącz */


void FtpWindow::enableConnectButton()
{
    // zapisywania uzytej konfiguracji

    QNetworkConfiguration config = networkSession->configuration();
    QString id;
    if (config.type() == QNetworkConfiguration::UserChoice)
        id = networkSession->sessionProperty(QLatin1String("UserChoiceConfiguration")).toString();
    else
        id = config.identifier();

    QSettings settings(QSettings::UserScope, QLatin1String("Trolltech"));
    settings.beginGroup(QLatin1String("QtNetwork"));
    settings.setValue(QLatin1String("DefaultNetworkConfiguration"), id);
    settings.endGroup();

    connectButton->setEnabled(networkSession->isOpen());
    connectAct->setEnabled(networkSession->isOpen());
    statusLabel->setText(tr("Proszę podać nazwę serwer FTP."));
}

